package nl.pcnerd.roelve.javagame;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

import nl.pcnerd.roelve.javagame.map.MapLoader;

public class Game extends JPanel implements Runnable {

	private static final long serialVersionUID = 1L;

	// Constants -- Widht and Height are defined on a 32*32 tile base, don't
	// fuck up here or shit will crash, use multiples of the tile size!
	public static final int TILE_SIZE = 32;
	public static final int HTILES = 20;
	public static final int VTILES = 15;

	private static final int WIDTH = TILE_SIZE * HTILES;
	private static final int HEIGHT = TILE_SIZE * VTILES;

	// Image buffering
	private Image dbImage;
	private Graphics dbGraphics;

	private MapLoader map;

	public Game() {
		// Setting up the size of our Game.
		map = new MapLoader();
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		setLayout(null);
		Thread gameLoop = new Thread(this);
		gameLoop.start();
	}

	/**
	 * This will keep the game logic going. TODO work this out
	 */
	@Override
	public void run() {

	}

	/**
	 * This method provides our double buffering, should not be looked at ever
	 * again unless we want to triple buffer. Or maybe if we wanted to use
	 * Graphics2D and BufferStrategy.
	 */
	@Override
	public void paint(Graphics g) {
		dbImage = createImage(WIDTH, HEIGHT);
		dbGraphics = dbImage.getGraphics();
		paintComponent(dbGraphics);
		g.drawImage(dbImage, 0, 0, this);
	}

	/**
	 * This method draws stuff on the screen based on the game logic.
	 */
	@Override
	protected void paintComponent(Graphics dbg) {
		map.drawBase(dbg);
		repaint();
	}

}
